//
//  StarView.m
//  UI作业21_豆瓣collectionView_彭安东
//
//  Created by 彭安东 on 15/10/15.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "StarView.h"

@interface StarView ()
@property (nonatomic, retain) NSArray *starArr;
@end

@implementation StarView

- (instancetype)initWithFrame:(CGRect)frame rating:(float)rating
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        for (NSInteger i = 0; i < 5; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(width / 5 * i, 0, width / 5, height)];
            if (rating >= 1.0) {
                imgView.image = [UIImage imageNamed:@"star_red"];
            }else if (rating >= 0.5){
                imgView.image = [UIImage imageNamed:@"star_half"];
            }else if (rating < 0.5){
                imgView.image = [UIImage imageNamed:@"star_white"];
            }
            rating -= 1;
            [self addSubview:imgView];
        }
    }
    return self;
}

- (void)setRating:(float)rating
{
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(14 * i, 5, 14, 14)];
        if (rating >= 1.0) {
            imgView.image = [UIImage imageNamed:@"star_red"];
        }else if (rating >= 0.5){
            imgView.image = [UIImage imageNamed:@"star_half"];
           
        }else if (rating < 0.5){
            imgView.image = [UIImage imageNamed:@"star_white"];
        }
        rating -= 1;
        [self addSubview:imgView];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
